package com.vc.util;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;


public class NetworkUtil {
	
	/**
	 * 网络类型
	 * @author Administrator
	 *
	 */
    interface NetType
    {
    	int NONE = 0;
    	int WIFI = 1;
    	int G2 = 2;
    	int G3 = 3;
    	int G4 = 4;  
    	int CABLE = 5;
    }
	
    
    /**
     * 获取网络类型
     * @param context
     * @return
     */
	public static int getSystemNetwork(Context context)
    {
        int netType = NetType.NONE;
       /* ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info = mgr.getActiveNetworkInfo();
        int connectType = ConnectivityManager.TYPE_DUMMY;
        int mobileType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
        if (info != null && info.isAvailable())
        {
            connectType = info.getType();
            switch (connectType)
            {
            case ConnectivityManager.TYPE_ETHERNET:
                netType = NetType.CABLE;
                break;

            case ConnectivityManager.TYPE_WIFI:
            case ConnectivityManager.TYPE_WIMAX:
                netType = NetType.WIFI;
                break;

            case ConnectivityManager.TYPE_MOBILE:
            case ConnectivityManager.TYPE_MOBILE_DUN:
            case ConnectivityManager.TYPE_MOBILE_SUPL:
            case ConnectivityManager.TYPE_MOBILE_MMS:
            case ConnectivityManager.TYPE_MOBILE_HIPRI:
            {
                TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                mobileType = tmgr.getNetworkType();
                switch (mobileType)
                {
                case TelephonyManager.NETWORK_TYPE_LTE://
                    // TODO 4g
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    netType = NetType.G3;
                    break;

                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:

                case TelephonyManager.NETWORK_TYPE_UNKNOWN:

                default:
                    netType = NetType.G2;
                    break;
                }
            }
                break;
            default:
                break;
            }
        }*/
        return netType;
    }
	
	
	public static boolean isAirplaneModeOn(Context context) {  
		return Settings.System.getInt(context.getContentResolver(),  
				Settings.System.AIRPLANE_MODE_ON, 0) != 0;  	    
	} 
	
	
	/**
	 * 判断当前网络连接是否为WIFI
	 * @param context
	 * @return
	 */
	public static boolean isWifiEnabled(Context context) {   
		try {      
		    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);      
		    NetworkInfo info = cm.getActiveNetworkInfo();      
		    String typeName = info.getTypeName().toLowerCase(); // WIFI/MOBILE      
		    if(typeName.equals("wifi")){  
		    	return true;
		    }else{     
			    return false;   
		    }        
		} catch (Exception e) {
			return false;
		}
	}
		
	/**判断此网络是否为mobile
	 * 注：适应三星新定义的双卡双待mobile类型
	 * @param netInfo
	 * @return
	 */
	public static boolean isMobileNetworkInfo(final NetworkInfo netInfo) {
		if(ConnectivityManager.TYPE_MOBILE == netInfo.getType() 
				|| ConnectivityManager.TYPE_MOBILE + 50 == netInfo.getType()){
			return true;
		}else {
			return false;
		}
	}
	
	public static boolean is3Gor4G(Context context){
	    // 联通的3G UMTS HSDPA，移动和联通的2G为GPRS/EGDE，电信2G为CDMA，电信的3G为EVDO
	    try{
	        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	        int type = tm.getNetworkType();
	        Log.d("is3Gor4G", "type:"+type);
	        /*if(type==TelephonyManager.NETWORK_TYPE_HSDPA || type==TelephonyManager.NETWORK_TYPE_LTE
	            || type == TelephonyManager.NETWORK_TYPE_UMTS || type== TelephonyManager.NETWORK_TYPE_HSPAP
	            || type == TelephonyManager.NETWORK_TYPE_HSPA
	            || type == TelephonyManager.NETWORK_TYPE_EHRPD || type== TelephonyManager.NETWORK_TYPE_EVDO_A 
	            || type== TelephonyManager.NETWORK_TYPE_HSUPA || type== TelephonyManager.NETWORK_TYPE_EVDO_B)*/
	        return true;
	    }catch(Exception e){
	        //权限？？
	    }
	    return false;
	}
	
	/**
	 * 是否支持网络
	 * @param context
	 * @return
	 */
    public static boolean isNetSupport(Context context) {
	    ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); 
	    if (cm == null)
	        return false;
	    
	    try
	    {
	    NetworkInfo[] info = cm.getAllNetworkInfo(); 
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    return true;
            }
        }
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	    }
	    return false;

    }
	
    /**
     * 获取网络类型
     * @param context
     * @return
     */
	public static int getNetworkType(Context context){
	    int type = -1;
	    ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            if (networkInfo != null) {
                type = networkInfo.getType();
            }
        }
        return type;
    }
	


	/**
	 * 网络是否可用
	 * @param context
	 * @return
	 */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connMgr.getActiveNetworkInfo();
        return info != null && info.isAvailable();
    }

    /**
     * 是否WIFI连接
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        if (context == null) {
            return false;
        }
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }
    
    /**
     * 获取代表联网状态的NetWorkInfo对象
     * @param context
     * @return
     */
    public static NetworkInfo getActiveNetworkInfo(Context context) {
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        return connMgr.getActiveNetworkInfo();
    }
}
